package com.example.myapplication.adapter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.MyMediaPlayer;
import com.example.myapplication.R;
import com.example.myapplication.utils.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {

    private JSONArray list;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView videoImg;
        TextView videoName;

        public ViewHolder (View view) {
            super(view);
            videoImg = (ImageView) view.findViewById(R.id.video_image);
            videoName = (TextView) view.findViewById(R.id.video_name);
        }

    }

    public VideoAdapter(JSONArray list){
        this.list = list;
    }

    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        //ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        //layoutParams.width=Math.round(parent.getWidth()/6);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,int position){
        try {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(view.getContext(), MyMediaPlayer.class);
                    try {
                        intent.putExtra("classify",list.getJSONObject(holder.getAdapterPosition()).getString("classify"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    view.getContext().startActivity(intent);
                }
            });
            JSONObject video = list.getJSONObject(position);
            holder.videoImg.setImageResource(R.drawable.videoimg);
            /*Bitmap bitmap = CacheHelper.sLruCache.get (video.getString("imgurl"));
            if (bitmap == null) {
                new ImageTask (new ImageTask.Listener () {
                    @Override
                    public void onSuccess(Bitmap bitmap) {
                        holder.videoImg.setImageBitmap (bitmap);
                    }
                }).execute (video.getString("imgurl"), video.getString("imgurl"));
            }else {
                holder.videoImg.setImageBitmap (bitmap);
            }*/



            //holder.videoImg.setImageResource(R.drawable.city);
            holder.videoName.setText(video.getString("classify"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount(){
        return list.length();
    }

    private static final class ImageTask extends AsyncTask<String, Void, Bitmap> {
        private Listener mListener;
        ImageTask(Listener listener) {
            mListener = listener;
        }
        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap bitmap = Request.bmp(strings[0]);
            CacheHelper.sLruCache.put (strings[1], bitmap);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            mListener.onSuccess (bitmap);
        }

        public interface Listener{
            void onSuccess(Bitmap bitmap);
        }

    }
    private static final class CacheHelper{
        private static LruCache<String, Bitmap> sLruCache;
        static {
            sLruCache = new LruCache<String, Bitmap>((int)Runtime.getRuntime ().maxMemory ()/4){
                @Override
                protected int sizeOf(String key, Bitmap value) {
                    return value.getByteCount ();
                }
            };
        }
    }

}
